<?php
/**
 *  WB_CacheManager class unit tests
 *
 *  @author Jean-Lou Dupont
 *  @version @@package-version@@
 */

require_once 'PHPUnit/Framework.php';
require_once 'web-bloks/CacheManager/CacheManager.php';
require_once 'web-bloks/ObjFactory/ObjFactory.php';
require_once 'web-bloks/CachesMemory/CacheAPC.php';
require_once 'web-bloks/CachesMemory/CacheeAccel.php';

class UnitTest extends PHPUnit_Framework_TestCase
{
	/**
	 * @private
	 */
	var $cm = null;
	var $cache = null;
	
	public function setup()
	{
		$this->cm = WB_ObjFactory::factory( 'WB_CacheManager' );
		$this->cache = $this->cm->getCache();
	}
	
	public function testAvailableCaches()
	{
		$caches = $this->cm->getAvailableCaches();
		$this->assertEquals( is_array( $caches ), true );
		#var_dump( $caches );
	}
	
	public function testRegisteredClasses()
	{
		$result = true;
		try
		{
			$cache = $this->cm->getCache();
			$type = $cache instanceof WB_CacheClass;
			$classe = get_class( $cache );
		}
		catch( Exception $e )
		{
			$result = false;			
		}
		
        $this->assertEquals( $result, true );		
        $this->assertEquals( $type, true );
		echo "Cache class: $classe \n";
		
	}
	

	public function testSingleton()
	{
		$cm2 = WB_ObjFactory::factory( 'WB_CacheManager' );		
		
		$this->assertEquals( $this->cm, $cm2 );				
	}

	// ===============================================================

    public function testGetCacheInfo()
    {
		$i = $this->cache->getInfo();
		
		#var_dump( $i );
		
        $this->assertEquals( true, is_array( $i ) );
    }
	
    public function testGetInvalidKeyFromCache()
    {
		$r = $this->cache->get( 'invalidkey' );
		
        $this->assertEquals( false, $r );
    }
    public function testWriteValidToCache1()
    {
		$r = $this->cache->set( 'key1', 'value1' );
		
        $this->assertEquals( true, $r );
    }
    public function testWriteValidToCache2()
    {
		$this->cache->set( 'key1', 'value1' );
		$r = $this->cache->get( 'key1' );	
		
        $this->assertEquals( 'value1', $r );
    }
    public function testReplace()
    {
	
		$this->cache->replace( 'key1', 'value2' );
		
		$r = $this->cache->get( 'key1' );		
		
        $this->assertEquals( 'value2', $r );
    }
    public function testAddToExistingKey()
    {
		// testing adding an already existing key.
		$r = $this->cache->add( 'key1', 'value3' );
		
        $this->assertEquals( false, $r );
    }
    public function testAddNonExistingKey()
    {
		$this->cache->delete( 'key3' );
		
		// try adding a non-existing key
		$r = $this->cache->add( 'key3', 'value2' );
		
        $this->assertEquals( true, $r );
    }
    public function testDeleteNonExistingKey()
    {
		// try deleting a non-existing key
		/*$r =*/ $this->cache->delete( 'key4' );
		$r = $this->cache->get( 'key4' );
		
        $this->assertEquals( false, $r );
    }

} // end class